/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.controller.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.jdesktop.application.ResourceMap;

import de.pd.pjim2.util.FileUtil;

/**
 * @author petros
 *
 */
@SuppressWarnings("serial")
public class ProcessedFolderPanel extends JPanel {

	private JProgressBar progBar;
	private File folder;
	private Icon mOkIcon;
	private JLabel mLblOk;
	
	public ProcessedFolderPanel(File pFolder) {
		folder = pFolder;
		
		ApplicationActionMap actionMap = Application.getInstance().getContext().getActionMap(this);
		ResourceMap resourceMap = Application.getInstance().getContext().getResourceMap();
		
		setPreferredSize(new Dimension(200, 50));
		setBackground(new Color(238, 238, 238));
		setLayout(null);

		// icon
		JButton btnFolder = new JButton(actionMap.get("openFolderAction"));
		btnFolder.setBounds(0, 0, 32, 32);
		btnFolder.setBorder(null);
		add(btnFolder);
		
		// label
		JLabel lblPath = new JLabel(pFolder.getName());
		lblPath.setBounds(35, 5, 150, 20);
		add(lblPath);
		
		mOkIcon = resourceMap.getIcon("okIcon");
		mLblOk = new JLabel(mOkIcon);
		Rectangle iconRect = new Rectangle(lblPath.getX() + lblPath.getWidth() + 5, 0, 70, 36);
		mLblOk.setBounds(iconRect);
		mLblOk.setVisible(false);
		add(mLblOk);
		
		progBar = new JProgressBar();
		progBar.setBounds(iconRect);
		add(progBar);
	}

	public JProgressBar getProgBar() {
		return progBar;
	}

	public void setProgBar(JProgressBar pProgBar) {
		progBar = pProgBar;
	}
	
	public void startedProcessing(int pMax) {
		progBar.setVisible(true);
		progBar.setMaximum(pMax);
		progBar.setValue(0);
		mLblOk.setVisible(false);
	}
	
	public void finishedProcessing() {
		progBar.setVisible(false);
		mLblOk.setVisible(true);
	}
	
	@Action
	public void openFolderAction() {
		try {
			FileUtil.openFolder(folder);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(this, "Could not open directory", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
}
